//============================================================================
// Name        : Stringline.c
// Author      : Michael Gower, Sameer Jamal, Kevin Ross
// Version     :
// Copyright   : GPL
// Description : Program to interpret Stringline language
//============================================================================

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "objects/File/File.h"
#include "objects/String/String.h"

extern FILE* yyin;
extern FILE* yyout;
extern int gC90_parseError;


int main(int argc, char** argv)
{
	FILE* pInputFile;
	FILE* pBitBucket = fopen("temp.txt", "w");
	if (!pBitBucket) {
		printf("Unable to open temp.txt\n");
		return 1; // fail
	}
	FILE* origStdout = stdout;
	stdout = pBitBucket;
	if (argc == 2)
	{
		pInputFile = fopen(argv[1], "r");
		if (pInputFile)
		{
			yyin = pInputFile;
			yyrestart(yyin);
			yyout = pBitBucket;
			//printf("Parsing file: %s\n", argv[1]);
		}
		else
		{
			printf("Unable to open file: %s\n", argv[1]);
			return 1; // fail
		}
	}

	yyparse();
	stdout = origStdout;
	fclose(pInputFile);
	fclose(pBitBucket);
	if (gC90_parseError)
	{
		printf("Syntax Error in Parse.\n");
		return 1; // fail
	}

	if (argc == 2) {
		pFile input = File_new(String_new(argv[1]), String_new("r"));
		assert(input);
		pString inputContents = File_readAll(input);
		assert(inputContents);
		printf("%s\n", inputContents->cString);
	}
	return 0; // no parse error, success
}
